package dbsucxentW;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class SucxentLoad {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//default username / pwd
		String userName = "ntu";
		String pwd = "ntu1234";

		if (args.length != 3)
		{
			System.out.println("SucxentLoad");
			System.out.println("Syntax:");
			System.out.println("  <input filename> <input dbName> <0 (Sucxent) or 1 (Edge)>");
			System.out.println("Example:");
			System.out.println("java -jar sucxent.20070312MINT.jar \"d:\\sucxent\\data\\bib.xml\" \"SucxentDB\" 0");
			System.out.println("\tUserName & pwd are taken from file dbuserpwd.txt");
			System.out.println("\tIf the file doesn't exist, default UserName & pwd: "+userName+" / "+pwd);
			System.out.println();
			System.exit(0);
		}
		
		String filename = args[0];
		String dbName = args[1];
		int mode = Integer.parseInt(args[2]);
		Sucxent sucxent;
		
		sucxent = new Sucxent();
		try{
    		File test = new File("dbuserpwd.txt");
    		try {
    			System.out.println("test file");
    			if (test.exists()) {
        			System.out.println("test exists");
    	            BufferedReader in = new BufferedReader(new FileReader("dbuserpwd.txt"));
        			System.out.println("Buffered Reader");
        			String str;
    	            if ((str = in.readLine()) != null) {
    	            	userName = str;
    	            	pwd = in.readLine();
    	            }	        
    			} 
        	} catch (IOException eIO) {
        		System.err.println("Error reading file:" + eIO);
        	}

	        
			//sucxent.addDatabase(String userName, String pass, String db, int mode)
			sucxent.addDatabase(userName, pwd, dbName, mode);
			System.out.println("addDatabase");
			
			//sucxent.connect(db.getUser(), db.getPass(), db.getName(), db.getMode());
			sucxent.connect(userName, pwd, dbName, mode);
			System.out.println("connect");
			
			sucxent.loadDocument(filename);
			System.out.println("add File");
		}
		catch (Exception e){
			System.err.println("Error loading doc:" + e);
		}
	}

}
